package com.sherlocky.oss.qiniu.web;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.rework.joss.persistence.convention.ConventionUtils;
import com.sherlocky.common.BaseController;
import com.sherlocky.oss.qiniu.bean.QnNotifyDTO;
import com.sherlocky.oss.qiniu.handler.QiniuOSSHandler;
import com.sherlocky.oss.qiniu.util.QiniuOSSUtil;
import com.sherlocky.resource.service.ResourceService;
import com.sherlocky.util.JsonUtil;

@Controller
@RequestMapping("/oss/qiniu")
public class QiniuOSSController extends BaseController {
	private static Log log = LogFactory.getLog(QiniuOSSController.class);
	@Autowired
	private ResourceService resourceService;
	
	@RequestMapping(value="/token", method = RequestMethod.GET)
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		sendJsonMessage(response, ConventionUtils.toMap("uptoken", QiniuOSSUtil.getUpToken(QiniuOSSHandler.getOSSBucket())));
	}
	
	@RequestMapping(value="/save/file", method = RequestMethod.POST)
	public void saveResource(@RequestParam String hash, @RequestParam String key, @RequestParam String domain, @RequestParam Long size, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		resourceService.saveResource(hash, key, domain, size);
		//TODO 请求七牛转码
		// sendJsonMessage(response, ConventionUtils.toMap("uptoken", QiniuOSSUtil.getUpToken(QiniuOSSHandler.getOSSBucket())));
	}
	
	@RequestMapping(value="/persistent/notify", method = RequestMethod.POST)
	public void persistentNotifyUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.info("======转码通知接口======");
		try {
			ServletInputStream in = request.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] b = new byte[4096];
			int i = 0;
			while ((i = in.read(b)) > 0) {
				out.write(b, 0, i);
			}
			byte[] r = out.toByteArray();
			String jsonStr = new String(r, "UTF-8");
			
			QnNotifyDTO qnDTO = (QnNotifyDTO) JsonUtil.toBean(jsonStr, QnNotifyDTO.class);
			log.info("返回的JSON：" + jsonStr);
			log.info("任务ID：" + qnDTO.getId());
			log.info("inputKey：" + qnDTO.getInputKey());
			String id = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(qnDTO.getInputKey(), "/"), ".");
			//TODO
			// 可自定义 参数 String type = request.getParameter("type");
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("======通知结束======");
	}
}
